package cn.com.dhcc.turbo.web.controller.exch;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu.LEAF_MENU;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo;
import cn.com.dhcc.turbo.service.exch.exhmgr.SynchronizeInfoService;
/**
 * 信息同步
 * 
 * @日期：2014-6-6 上午9:26:54
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Controller
@RequestMapping("/exch")
@TurboMenu(belongToMenu=LEAF_MENU.SYNC_MGR)
public class SynchronizeInfoController extends BaseController {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);
	
	@Autowired
	private SynchronizeInfoService syncService;
	
	@RequestMapping(value = "/sync_info_list")
	public String list(Model model, Pager pager,String dataType,String SyncOperType,String status) {
		WhereCondition condition = new WhereCondition();
		if(status == null || "".equals(status)){
			status = "0";
		}
		condition.where().eq("t1.status", status);
		if (dataType != null && !"".equals(dataType) ) {
			condition.and().eq("t1.data_type",dataType);
		}
		if (SyncOperType != null && !"".equals(SyncOperType) ) {
			condition.and().eq("t1.oper_type",SyncOperType);
		}
		condition.orderBy("t1.mtime desc");
		try {
			PagerData<SynchronizeInfo> pagerData = syncService.list(pager,condition);
			model.addAttribute(DATA_KEY, pagerData.getDatas());
			setTotoal(model, pagerData.getTotal());
		} catch (Exception e) {
			logger.error("获取同步信息失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "exch/sync_info_list";
	}
	@RequestMapping(value = "/sync_info_to_rel_node")
	@ResponseBody
	public Map<String, Object> syncToRelNode(Model model,String id) {
		try {
			SynchronizeInfo sync = syncService.get(id);
			sync.setStatus("3");
			syncService.reSync(sync);
			return jsonSuccessResult("同步成功");
		} catch (Exception e) {
			logger.error("同步失败", e);
			return jsonFailResult("同步失败");
		}
	}
	@RequestMapping(value = "/re_sync")
	@ResponseBody
	public Map<String, Object> reSync(Model model) {
		try {
			syncService.reSync();
			return jsonSuccessResult("同步成功");
		} catch (Exception e) {
			logger.error("同步失败", e);
			return jsonFailResult("同步失败");
		}
	} 
}
